package cc.comac.mom.webservice.impl;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.ans.shaded.com.alibaba.fastjson.JSONArray;
import com.alibaba.ans.shaded.com.alibaba.fastjson.JSONObject;

import cc.comac.mom.bean.QualityQarSelectBean;
import cc.comac.mom.bean.QualityQarSelectReturnBean;
import cc.comac.mom.bean.QualityResultBean;
import cc.comac.mom.datahandlers.entity.QualityQarCheckInvoiceDataHandler;
import cc.comac.mom.datahandlers.entity.QualityQarVerifyDataHandler;
import cc.comac.mom.entity.QualityQarVerifyEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.webservice.QualityCorrectionPlatformWebService;

@WebService(serviceName = "CorrectionPlatform", targetNamespace = "http://webservice.mom.comac.cc/", endpointInterface = "cc.comac.mom.webservice.QualityCorrectionPlatformWebService")
@Component
@Transactional
public class QualityCorrectionPlatformWebServiceImpl implements QualityCorrectionPlatformWebService {

	@Autowired
	private QualityQarCheckInvoiceDataHandler qualityQarCheckInvoiceDataHandler;
	@Autowired
	private QualityQarVerifyDataHandler qualityQarVerifyDataHandler;

	//查询qar信息
	@Override
	public String searchOutLineInfoByQar(String json) {
		JSONObject parseObject = JSONObject.parseObject(json.toString());
		// 件号
		String partNo = parseObject.getString("partno");
		// 状态 1:查已发布未提交审核的 2:查全部 3 :查待发布、已审核的、已归档
		Integer status = Integer.valueOf(parseObject.getString("status"));
		// 条数
		Integer fetchNum = Integer.valueOf(parseObject.getString("fetch"));
		// 开始条数
		Integer startNum = Integer.valueOf(parseObject.getString("start"));

		QualityQarSelectBean equipQarSelectBean = new QualityQarSelectBean();
		equipQarSelectBean.setPartNo(partNo);
		equipQarSelectBean.setStatus(status.intValue());
		equipQarSelectBean.setStartNUm(startNum.intValue());
		equipQarSelectBean.setFetchNum(fetchNum.intValue());
		QualityQarSelectReturnBean selectByFieldsForWeb = qualityQarCheckInvoiceDataHandler
				.selectByFieldsForWeb(equipQarSelectBean);
		String string = selectByFieldsForWeb.toString();
		return string;
	}

	//插入qar验证信息
	@Override
	public String setCorrectiveActionByQar(String json) {
		QualityResultBean qualityResultBean = new QualityResultBean();
		JSONObject parseObject = JSONObject.parseObject(json.toString());
		String project = parseObject.getString("project");
		String sid = parseObject.getString("sid");
		JSONArray jsonArray = parseObject.getJSONArray("qar");
		if (jsonArray == null || jsonArray.size() < 1) {
			qualityResultBean.setMsg("qar信息为空");
			qualityResultBean.setStatus("0");
			return qualityResultBean.toString();
		}
		// 校验纠正措施平台传入的qar是否在已发布，检验中，委托中状态，当不是时返回数据
		List<String> qarList = new ArrayList<String>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			String qarno = jsonObject.getString("qarno");
			qarList.add(qarno);
		}
		if (qarList.size() > 0) {
			ResultBean validateQarStatus = qualityQarCheckInvoiceDataHandler.validateQarStatus(qarList);
			if (!validateQarStatus.success) {
				qualityResultBean.setMsg(validateQarStatus.getMessage());
				qualityResultBean.setStatus("0");
				return qualityResultBean.toString();
			}
		}
		ArrayList<QualityQarVerifyEntity> listEntity = new ArrayList<QualityQarVerifyEntity>();
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			String qarno = jsonObject.getString("qarno");
			QualityQarVerifyEntity qualityQarVerifyEntity = new QualityQarVerifyEntity();
			qualityQarVerifyEntity.setProject(project);
			qualityQarVerifyEntity.setSid(sid);
			qualityQarVerifyEntity.setQarno(qarno);
			listEntity.add(qualityQarVerifyEntity);
		}
		ResultBean insertBatch = qualityQarVerifyDataHandler.insertBatch(listEntity);
		if (!insertBatch.success) {
			qualityResultBean.setMsg(insertBatch.getMessage());
			qualityResultBean.setStatus("0");
			return qualityResultBean.toString();
		}
		qualityResultBean.setMsg("成功");
		qualityResultBean.setStatus("1");
		return qualityResultBean.toString();
	}

}
